import Vue from 'vue'
import App from './App'
import store from "./store"
import verification from './utils/verification.js'
import moment from './utils/moment.js'
import loading from './utils/loading.js'
import uploadImage from './utils/upload-image.js'
import noData from './pages/components/public/no-data.vue'
import control from './utils/control.js'
import imgPreview from './utils/img-preview.js'
import imageCompress from './pages/components/public/image-compress.vue'

import COS from './utils/cos-wx-sdk-v5.js'
import TIM from './utils/tim-wx.js'
import TYPES from './utils/types.js'
import orangeUtil from './utils/orange-util/orange-util.js'; 
import uView from "uview-ui";
import throttle from './utils/throttle.js'
import {baseHost,options} from './setting.js'


const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {
  // 请求完新版本信息的回调
  console.log(res.hasUpdate)
})

updateManager.onUpdateReady(function () {
  wx.showModal({
    title: '更新提示',
    content: '新版本已经准备好，是否重启应用？',
    success(res) {
      if (res.confirm) {
        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
        updateManager.applyUpdate()
      }
    }
  })
})

updateManager.onUpdateFailed(function () {
  // 新版本下载失败
})


App.mpType = 'app' 
Vue.use(uView);

Vue.config.productionTip = false
Vue.component('no-data', noData)
Vue.component('image-compress', imageCompress)

Vue.prototype.$verification = verification
Vue.prototype.$moment = moment
Vue.prototype.$loading = loading
Vue.prototype.$uploadImage = uploadImage
Vue.prototype.$throttle = control.throttle
Vue.prototype.$debounce = control.debounce
Vue.prototype.$baseHost = baseHost
Vue.prototype.$imgPreview = imgPreview

const tim = TIM.create({
	SDKAppID: options.SDKAPPID
})

tim.setLogLevel(4)

uni.$Tim = tim
uni.$Tim.registerPlugin({
	'cos-wx-sdk': COS
})

uni.TIM = TIM

const app = new Vue({
	...App,
	store
})


Vue.prototype.TIM = TIM
Vue.prototype.$type = TYPES

tim.on(TIM.EVENT.SDK_READY, onReadyStateUpdate, this)
tim.on(TIM.EVENT.SDK_NOT_READY, onReadyStateUpdate, this)

tim.on(TIM.EVENT.KICKED_OUT, kickOut, this)
// 出错统一处理
tim.on(TIM.EVENT.ERROR, onError, this)

tim.on(TIM.EVENT.MESSAGE_RECEIVED, messageReceived, this)
tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, convListUpdate, this)
tim.on(TIM.EVENT.GROUP_LIST_UPDATED, throttle(groupListUpdate,1000), this) // 使用节流，解决第二次更新为空的问题
tim.on(TIM.EVENT.BLACKLIST_UPDATED, blackListUpdate, this)
tim.on(TIM.EVENT.GROUP_SYSTEM_NOTICE_RECEIVED, groupSystemNoticeUpdate, this)

function onReadyStateUpdate({
	name
}) {
	// console.log('**********',name, TIM.EVENT.SDK_READY, name === TIM.EVENT.SDK_READY)
	const isSDKReady = (name === TIM.EVENT.SDK_READY)
	if (isSDKReady) {
		uni.$Tim.getMyProfile().then(res => { // 获取个人信息
			store.commit('tim/UPDATE_MY_INFO', res.data) // 存储个人信息
		})
		uni.$Tim.getBlacklist().then(res => {
			store.commit('tim/SET_BLACK_LIST', res.data)
		})
	}
	store.commit('tim/SET_SDK_READY', isSDKReady)
}

function kickOut(event) {
	store.dispatch('tim/resetStore')
	uni.showToast({
		title: '你已被踢下线',
		icon: 'none',
		duration: 1500
	})
	setTimeout(() => {
		uni.reLaunch({
			url: '../login/main'
		})
	}, 500)
}

function onError(event) {
	// 网络错误不弹toast && sdk未初始化完全报错
	if (event.data.message && event.data.code && event.data.code !== 2800 && event.data.code !== 2999) {
		store.commit('tim/SHOW_TOAST', {
			title: event.data.message,
			duration: 2000
		})
	}
}

function messageReceived(event) {
	for (let i = 0; i < event.data.length; i++) {
		let item = event.data[i]
		if (item.type === TYPES.MSG_GRP_TIP) {
			if (item.payload.operationType) {
				app.$emit('groupNameUpdate', item.payload)
			}
		}
		// if (item.type === TYPES.MSG_CUSTOM) {
		// 	if (isJSON(item.payload.data)) {
		// 		const videoCustom = JSON.parse(item.payload.data)
		// 		if (videoCustom.version === 3) {
		// 			switch (videoCustom.action) {
		// 				// 对方呼叫我
		// 				case 0:
		// 					if (!store.getters.isCalling) {
		// 						let url = `../call/main?args=${item.payload.data}&&from=${item.from}&&to=${item.to}`
		// 						uni.navigateTo({
		// 							url
		// 						})
		// 					} else {
		// 						app.$emit('isCalling', item)
		// 					}
		// 					break
		// 					// 对方取消
		// 				case 1:
		// 					uni.navigateBack({
		// 						delta: 1
		// 					})
		// 					break
		// 					// 对方拒绝
		// 				case 2:
		// 					app.$emit('onRefuse')
		// 					break
		// 					// 对方不接1min
		// 				case 3:
		// 					uni.navigateBack({
		// 						delta: 1
		// 					})
		// 					break
		// 					// 对方接听
		// 				case 4:
		// 					app.$emit('onCall', videoCustom)
		// 					break
		// 					// 对方挂断
		// 				case 5:
		// 					app.$emit('onClose')
		// 					break
		// 					// 对方正在通话中
		// 				case 6:
		// 					app.$emit('onBusy')
		// 					break
		// 				default:
		// 					break
		// 			}
		// 		}
		// 	}
		// }
	}
	store.dispatch('tim/onMessageEvent', event)
}

function convListUpdate(event) {
	store.commit('tim/UPDATE_ALL_CONVERSATION', event.data)
}

function groupListUpdate(event) {
	console.log('UPDATE_GROUP_LIST',event.data)
	store.commit('tim/UPDATE_GROUP_LIST', event.data)
	
}

function blackListUpdate(event) {
	store.commit('tim/UPDATE_BLACK_LIST', event.data)
}

function groupSystemNoticeUpdate(event) {
	console.log('system message', event)
}

// 获取系统信息
let sysInfo = uni.getSystemInfoSync()
store.commit('tim/SET_SYSTEM__INFO', sysInfo)


App.mpType = 'app'

Vue.use(orangeUtil);

app.$mount()
